package com.moleq.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.moleq.adapter.ReceiptSetupAdapter.ViewHolder;
import com.moleq.base.EditableListener;
import com.moleq.mgpos.activities.R;
import com.moleq.models.TareSetting;
import com.moleq.util.MoleQUtil;

public class TareSetupAdapter extends MQBaseAdapter {
	
	public static final String TARE_EDITED = "TareEdit";
	private LayoutInflater mInflater;
	private List<TareSetting> listTare;
	private EditableListener mEditableListener;
	
	public TareSetupAdapter(Context context,List<TareSetting> listTare){
		this.mInflater = LayoutInflater.from(context);
		this.listTare = listTare;
	}
	
	public void setEditableListener(EditableListener listener){
		mEditableListener = listener;
	}
	
	public void setSource(List<TareSetting> listTare){
		this.listTare = listTare;
	}

	public int getCount() {
		return listTare.size();
	}

	public Object getItem(int position) {
		return listTare.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		super.getView(position, convertView, parent);
		
		ViewHolder holder = null;
//		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.ps_tare_row, null);
			holder.tv_tareno = (TextView) convertView.findViewById(R.id.tv_tareno);
			holder.tv_taredesc = (TextView) convertView.findViewById(R.id.tv_taredesc);
			holder.tv_tarevalue = (TextView) convertView.findViewById(R.id.tv_tarevalue);
			holder.tv_totalRate = (TextView) convertView.findViewById(R.id.tv_totalRate);
			holder.llTareRow = (LinearLayout) convertView.findViewById(R.id.llTareRow);
			
			convertView.setTag(holder);
//		}else {
//			holder = (ViewHolder) convertView.getTag();
//		}
		
		holder.tv_tareno.setText(String.valueOf(position + 1));
		listTare.get(position).setNo(position + 1);
		
		holder.tv_totalRate.setText(String.format("%.2f", listTare.get(position).getTotalTare()));
		holder.tv_taredesc.setText(listTare.get(position).getTa_desc());
		holder.tv_tarevalue.setText(String.valueOf(listTare.get(position).getTa_value()));
		
		if (position == selectedRow) {
			holder.llTareRow.setBackgroundColor(Color.rgb(255, 219, 16));
		}else {
			holder.llTareRow.setBackgroundColor(Color.rgb(204, 204, 204));
		}
		return convertView;
	}
	
	@Override
	public void onTextChange(int resId, String changedText) {
		
//		switch(resId){
//		case R.id.et_taredesc:
//			listTare.get(selectedRow).setTa_desc(changedText);
//			break;
//		case R.id.et_tarevalue:
//			if(MoleQUtil.isNumeric2(changedText)){
//				listTare.get(selectedRow).setTa_value(Integer.valueOf(changedText));
//			}else{
//				listTare.get(selectedRow).setTa_value(0);
//			}
//			break;
//		}
//		listTare.get(selectedRow).setEdit(true);
//		mEditableListener.onEdited(TARE_EDITED);
	}
	
		
	public final class ViewHolder{
		public TextView tv_tareno;
		public TextView tv_taredesc;
		public TextView tv_tarevalue;
		public TextView tv_totalRate;
		private LinearLayout llTareRow;
	}
	
}
